package com.example.xjt.restful;

import com.example.xjt.biz.UserService;
import com.example.xjt.event.MsgPub;
import com.example.xjt.orm.user.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * rest-api 编程案例
 */
@RestController  //等于 @Controller + @ResponseBody
public class HelloRestAPI {
    @Autowired
    private MsgPub msgPublisher;

    @GetMapping(path = "pub")
    public ResultVO publish(String msg) {
        msgPublisher.publish(msg);
        return ResultVO.ok();
    }

    //------------------------------
    @Autowired
    private UserService userService;

    @GetMapping("/user/{uid}")
    public ResultVO getById(@PathVariable Long uid) {
        return ResultVO.ok(userService.getById(uid));
    }

    @DeleteMapping("/user/{uid}")
    public ResultVO delById(@PathVariable Long uid) {
        userService.delUser(uid);
        return ResultVO.ok();
    }

    @PostMapping("/userInfo")
    public ResultVO createUser(@RequestBody UserDO userInfo) {
        return ResultVO.ok(userService.addUser(userInfo));
    }


    @PutMapping("/userInfo")
    public ResultVO alterUser(@RequestBody UserDO userInfo) {
        return ResultVO.ok(userService.updateUserInfo(userInfo));
    }


    //处理成功--且有返回数据
    @GetMapping("bigReq")
    public ResultVO bigReqList() {
        List<String> result = new ArrayList<>(2048);
        for (int i = 0; i < 2048; i++) {
            result.add(UUID.randomUUID().toString());
        }
        return ResultVO.ok(result);
    }


    //处理成功--没有返回数据
    @GetMapping("bigReq2")
    public ResultVO bigReq2() {
        System.err.println("处理成功--没有返回数据");
        return ResultVO.ok();
    }


    //自定义throw异常--测试例子
    //这种情况-Service层直接throw出自己定义的异常枚举就可以了
    //http://localhost:7910/api/throwExp?a=123&b=456
    //http://localhost:7910/api/throwExp?a=666&b=456
    @GetMapping("throwExp")
    public ResultVO throwExp(@RequestParam Integer a, Integer b) {
        //service业务层throw抛出的自定义异常，会直接被全局异常处理，而不会走正常情况下的return逻辑
        return ResultVO.ok(userService.judgeMoreThan999(a, b));
    }


    //多种错误返回情况的例子 ,适用于前端要显示多种错误情况的返回值
    //这种情况-不可避免的会把 ResultVO 带到Service层的代码中
    @GetMapping("mtFail")
    public ResultVO mtFail(@RequestParam Integer a, Integer b) {
        int r = a + b;
        if (r == 999) {
            return ResultVO.ok();
        } else if (r < 0) {
            return ResultVO.fail(ResultCodeEnum.BIZ_ERROR_1, r);
        } else if (r > 0 && r < 999) {
            return ResultVO.fail(ResultCodeEnum.BIZ_ERROR_2, r);
        } else {
            return ResultVO.fail(ResultCodeEnum.BIZ_ERROR, r);
        }
    }


    //受检异常--测试例子
    @GetMapping("checkExp")
    public ResultVO checkExp() {
        List<String> result = new ArrayList<>(2048);
        int a = 3 / 0; //测试除0异常会不会被处理
        for (int i = 0; i < 2048; i++) {
            result.add(UUID.randomUUID().toString());
        }
        return ResultVO.ok(result);
    }

    //测试受检异常-引发的事务回退
    @PutMapping("txError")
    public ResultVO txError() {
        userService.testError();
        return ResultVO.ok();
    }


    //测试-正常情况下修改操作
    @PutMapping("tx")
    public ResultVO tx() {
        userService.test();
        return ResultVO.ok();
    }

}